package com.hub.controller.asset;


import com.hub.controller.BaseController;
import com.hub.dto.asset.AssetInfoDto;
import com.hub.dto.asset.AssetInfoFileDto;
import com.hub.dto.asset.SearchAssetInfoDto;
import com.hub.service.asset.IAssetInfoFilesService;
import com.hub.utils.ApiResultObject;
import com.hub.utils.PagingResultObject;
import com.hub.service.asset.IAssetInfoService;
import com.hub.utils.enums.FileTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  资产基础信息前端控制器
 * </p>
 *
 * @author hub
 * @since 2020-05-18
 */

@Slf4j
@Api(value="资产基础信息相关API",tags={"资产基础信息" })
@RestController
@RequestMapping("/asset/info")
public class AssetInfoController extends BaseController {

    @Autowired
    private IAssetInfoService assetInfoService;

    @Autowired
    private IAssetInfoFilesService assetInfoFilesService;

    @ApiOperation("查询资产基础信息")
    @PostMapping(value = "/list")
    public PagingResultObject searchAssetInfo(@RequestBody SearchAssetInfoDto request){

        return assetInfoService.searchAssetInfoList(request);
    }

    @ApiOperation("资产基础信息详情")
    @GetMapping(value = "/{id}")
    public ApiResultObject getAssetInfo(@PathVariable long id){
        return this.assetInfoService.getAssetInfoDetail(id);
    }

    @ApiOperation("新增资产基础信息")
    @PostMapping(value = "")
    public ApiResultObject addAssetInfo(@RequestBody @Valid AssetInfoDto dto, BindingResult bindingResult){
        //数据验证
        ApiResultObject validResult = this.checkValidResult(bindingResult);
        if(!validResult.isSuccess()){
            return validResult;
        }

        return assetInfoService.addAssetInfo(dto);
    }

    @ApiOperation("修改资产基础信息")
    @PutMapping(value = "")
    public ApiResultObject modifyAssetInfo(@RequestBody @Valid AssetInfoDto dto, BindingResult bindingResult){
        //数据验证
        ApiResultObject validResult = this.checkValidResult(bindingResult);
        if(!validResult.isSuccess()){
            return validResult;
        }

        return assetInfoService.modifyAssetInfo(dto);
    }

    @ApiOperation("删除资产基础信息")
    @DeleteMapping(value = "/{id}")
    public ApiResultObject removeAssetInfo(@PathVariable long id){

        return assetInfoService.removeAssetInfo(id);
    }

    @ApiOperation("新增资产基础信息附件")
    @PostMapping(value = "/file")
    public ApiResultObject addAssetInfoFile(@RequestBody @Valid AssetInfoFileDto dto, BindingResult bindingResult){
        //数据验证
        ApiResultObject validResult = this.checkValidResult(bindingResult);
        if(!validResult.isSuccess()){
            return validResult;
        }

        return assetInfoFilesService.addAssetInfoFile(dto);
    }

    @ApiOperation("新增资产基础信息附件")
    @GetMapping(value = "{id}/file/type/{type}")
    public ApiResultObject loadAssetInfoFile(@PathVariable long id,@PathVariable int type){
        return ApiResultObject.success(assetInfoFilesService.loadAssetFilesByAssetIdAndType(id,FileTypeEnum.valueOf(type)));
    }

    @ApiOperation("删除资产基础信息附件")
    @DeleteMapping(value = "/file/{id}")
    public ApiResultObject removeAssetInfoFile(@PathVariable long id){

        return assetInfoFilesService.removeAssetInfoFile(id);
    }

}

